﻿using System;
using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using System.ComponentModel.DataAnnotations;
using HengTong.Model.Db.Other.Bill;
using HengTong.Model.Extension;
using TiaoTaoHelper.Wtm.Models;


namespace HengTong.ViewModel.Visitor.SysVisitorRecordRequestBillVms
{
    public partial class SysVisitorRecordRequestBillListVm : BaseListVm<SysVisitorRecordRequestBillView, SysVisitorRecordRequestBillSearcher>
    {

        protected override IEnumerable<IGridColumn<SysVisitorRecordRequestBillView>> InitGridHeader()
        {
            return new List<GridColumn<SysVisitorRecordRequestBillView>>{
                this.MakeGridHeader(x => x.VisitorReasonName),
                this.MakeGridHeader(x => x.Name),
                this.MakeGridHeader(x => x.Phone),
                this.MakeGridHeader(x => x.IdCardNo),
                this.MakeGridHeader(x => x.IdCardFrontPhotoId).SetFormat(IdCardFrontPhotoIdFormat),
                this.MakeGridHeader(x => x.IdCardBackPhotoId).SetFormat(IdCardBackPhotoIdFormat),
                this.MakeGridHeader(x => x.Company),
                this.MakeGridHeader(x => x.CarNo),
                this.MakeGridHeader(x => x.UserName),
                this.MakeGridHeader(x => x.ReservationTime),
                this.MakeGridHeader(x => x.Receptionist),
                this.MakeGridHeader(x => x.ReceptionistPhone),
                this.MakeGridHeader(x => x.RejectReason),
                this.MakeGridHeader(x => x.EntryTime),
                this.MakeGridHeader(x => x.LeaveTime),
                this.MakeGridHeader(x => x.Approver),
                this.MakeGridHeader(x => x.ApprovalTime),
                this.MakeGridHeader(x => x.GroupName),
                this.MakeGridHeader(x => x.BillCode),
                this.MakeGridHeader(x => x.BillStatus),
                this.MakeGridHeader(x => x.WorkflowStatus),
                this.MakeGridHeader(x => x.Remark),
                this.MakeGridHeader(x => x.CanApprove).SetHide().SetFormat((a, b) =>
                {
                    if(a.CanApprove) { return "true"; }
                    else { return "false"; }
                }),

                this.MakeGridHeaderAction(width: 200)
            };
        }
        private List<ColumnFormatInfo> IdCardFrontPhotoIdFormat(SysVisitorRecordRequestBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button,entity.IdCardFrontPhotoId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button,entity.IdCardFrontPhotoId,640,480),
            };
        }

        private List<ColumnFormatInfo> IdCardBackPhotoIdFormat(SysVisitorRecordRequestBillView entity, object val)
        {
            return new List<ColumnFormatInfo>
            {
                ColumnFormatInfo.MakeDownloadButton(ButtonTypesEnum.Button,entity.IdCardBackPhotoId),
                ColumnFormatInfo.MakeViewButton(ButtonTypesEnum.Button,entity.IdCardBackPhotoId,640,480),
            };
        }

        public override void AfterDoSearcher()
        {
            if (Wtm.LoginUserInfo != null)
            {
                List<SysWorkflow> ids = GetMyApproves();
                if (ids.Any())
                {
                    foreach (var item in EntityList)
                    {
                        var m = ids.Find(x => x.ModelId == item.GetID().ToString());
                        if (m != null && item.WorkflowStatus == WorkflowStatus.审核中)
                        {
                            item.CanApprove = true;
                        }
                    }
                }
            }
        }


        public override IOrderedQueryable<SysVisitorRecordRequestBillView> GetSearchQuery()
        {
            var query = DC.Set<SysVisitorRecordRequestBill>()
                .CheckEqual(Searcher.VisitorReasonId, x=>x.VisitorReasonId)
                .CheckContain(Searcher.Name, x=>x.Name)
                .CheckContain(Searcher.Phone, x=>x.Phone)
                .CheckContain(Searcher.IdCardNo, x=>x.IdCardNo)
                .CheckContain(Searcher.Company, x=>x.Company)
                .CheckContain(Searcher.CarNo, x=>x.CarNo)
                .CheckEqual(Searcher.UserId, x=>x.UserId)
                .CheckBetween(Searcher.ReservationTime?.GetStartTime(), Searcher.ReservationTime?.GetEndTime(), x => x.ReservationTime, includeMax: false)
                .CheckContain(Searcher.Receptionist, x=>x.Receptionist)
                .CheckContain(Searcher.ReceptionistPhone, x=>x.ReceptionistPhone)
                .CheckBetween(Searcher.EntryTime?.GetStartTime(), Searcher.EntryTime?.GetEndTime(), x => x.EntryTime, includeMax: false)
                .CheckBetween(Searcher.LeaveTime?.GetStartTime(), Searcher.LeaveTime?.GetEndTime(), x => x.LeaveTime, includeMax: false)
                .CheckEqual(Searcher.GroupId, x=>x.GroupId)
                .CheckContain(Searcher.BillCode, x=>x.BillCode)
                .CheckEqual(Searcher.BillStatus, x=>x.BillStatus)
                .CheckEqual(Searcher.WorkflowStatus, x=>x.WorkflowStatus)
                .CheckDataPrivilege(Wtm)
                .Select(x => new SysVisitorRecordRequestBillView
                {
				    ID = x.ID,
                    VisitorReasonName = x.VisitorReason.ReasonName,
                    Name = x.Name,
                    Phone = x.Phone,
                    IdCardNo = x.IdCardNo,
                    IdCardFrontPhotoId = x.IdCardFrontPhotoId,
                    IdCardBackPhotoId = x.IdCardBackPhotoId,
                    Company = x.Company,
                    CarNo = x.CarNo,
                    UserName = x.User.Name,
                    ReservationTime = x.ReservationTime,
                    Receptionist = x.Receptionist,
                    ReceptionistPhone = x.ReceptionistPhone,
                    RejectReason = x.RejectReason,
                    EntryTime = x.EntryTime,
                    LeaveTime = x.LeaveTime,
                    Approver = x.Approver,
                    ApprovalTime = x.ApprovalTime,
                    GroupName = x.Group.GroupName,
                    BillCode = x.BillCode,
                    BillStatus = x.BillStatus,
                    WorkflowStatus = x.WorkflowStatus,
                    Remark = x.Remark,
                })
                .OrderBy(x => x.ID);
            return query;
        }

    }

    public class SysVisitorRecordRequestBillView : SysVisitorRecordRequestBill{
        [Display(Name = "事由名称")]
        public String VisitorReasonName { get; set; }
        [Display(Name = "_Admin.Name")]
        public String UserName { get; set; }
        [Display(Name = "_Admin.GroupName")]
        public String GroupName { get; set; }
        public bool CanApprove { get; set; }


    }
}
